<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../react18/react.development.js"></script>
  <script src="../react18/react-dom.development.js"></script>
  <script src="../react18/babel.min.js"></script>
  <script src="../react18/prop-types.js"></script>
</head>

<body>
  <div id="app"></div>
  <script type="text/babel">
    /* 
      父子组件挂载流程:
        - 父组件constructor
        - 父组件componentWillMount
        - 父组件render
        - 子组件constructor
        - 子组件UNSAFE_componentWillMount
        - 子组件render
        - 子组件componentDidMount
        - 父组件componentDidMount

      父子组件更新流程:
        - 父组件:shouldComponentUpdate
        - 父组件:componentWillUpdate
        - 父组件:render
        - 子组件:componentWillReceiveProps(首次渲染不执行，以后只要父组件更新，都会执行)
        - 子组件:shouldComponentUpdate
        - 子组件:componentWillUpdate
        - 子组件:render
        - 子组件:componentDidUpdate
        - 父组件:componentDidUpdate

      父子组件的卸载流程:
        - 父组件:componentWillUnmount
        - 子组件:componentWillUnmount
    
    */
    class App extends React.Component{
      constructor(){
        super();
        console.log("****constructor****");
      }
      state = {
        count : 0
      }

      UNSAFE_componentWillMount(){
        console.log("****componentWillMount****");
      }
      render(){
        console.log("*****render*****");
        const {count} = this.state
        return (
          <div> 
            <h1>更新流程 {count}</h1>
            <button onClick={this.updateHandle}>按钮</button>  
            <button onClick={this.unmountHandle}>核按钮</button> 
            <br/>
            <Child/> 
          </div>
        )
      }
      updateHandle = () => {
        const {count} = this.state;
        this.setState({count:count + 1})
      }

      unmountHandle = () => {
        root.unmount();
      }


      componentDidMount(){
        console.log("****componentDidMount****");
      }

      shouldComponentUpdate(){
        console.log("****shouldComponentUpdate:组件是否应该更新****");
        return true;
      }

      UNSAFE_componentWillUpdate(){
        console.log("****componentWillUpdate:组件即将更新****");
      }
      

      componentDidUpdate(){
        console.log("****componentDidUpdate:组件已经更新完成****");
      }

      //常用:主要做收尾工作,清定时器 取消订阅 ...
      componentWillUnmount(){
        console.log("****componentWillUnmount:组件即将卸载****");
      }

    }



    class Child extends React.Component{
      constructor(){
        super();
        console.log("----constructor----");
      }

      UNSAFE_componentWillMount(){
        console.log("----UNSAFE_componentWillMount----");
      }

      render(){
        console.log("----render----");
        return (
          <div>
            <p>子组件</p>  
          </div>
        )
      }

      componentDidMount(){
        console.log("----componentDidMount----");
      }


      UNSAFE_componentWillReceiveProps(){
        console.log("----componentWillReceiveProps----");
      }

      shouldComponentUpdate(){
        console.log("----shouldComponentUpdate----");
        return true
      }

      UNSAFE_componentWillUpdate(){
        console.log("----componentWillUpdate----");
      }

      componentDidUpdate(){
        console.log("----componentDidUpdate----");
      }

      //常用:主要做收尾工作,清定时器 取消订阅 ...
      componentWillUnmount(){
        console.log("-----componentWillUnmount:组件即将卸载----");
      }
    }

    const root = ReactDOM.createRoot(document.getElementById('app'));
    root.render(<App/>)

  </script>
</body>

</html>